package com.ruoyi.produce.process.costApportionment.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.produce.process.costApportionment.domain.CostApportionmentCond;
import com.ruoyi.produce.process.costApportionment.domain.CostApportionmentDomain;
import com.ruoyi.produce.process.costApportionment.domain.CostApportionmentDto;
import com.ruoyi.produce.process.costApportionment.domain.CostApportionmentListDto;
import com.ruoyi.produce.process.costApportionment.service.ICostApportionmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 在产成本费用分摊
 */
@RestController
@RequestMapping("/produce/cost/apportionment")
@Api(value = "在产成本费用分摊模块", tags = "在产成本费用分摊接口")
public class CostApportionmentController extends BaseController {
    @Autowired
    private ICostApportionmentService costApportionmentService;

    /**
     * 获取在产成本费用分摊
     * @param cond
     * @return
     */
    @ApiOperation(value = "获取销售订单出库列表",notes = "查询所有销售订单出库信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody CostApportionmentCond cond) {
        startPage();
        List<CostApportionmentDto> costApportionmentDtos = costApportionmentService.selectCostApportionmentList(cond);
        return getDataTable(costApportionmentDtos);
    }

    /**
     * 导出在产成本费用分摊
     *
     * @param cond
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody CostApportionmentCond cond) {
        List<CostApportionmentDto> costApportionmentDtos = costApportionmentService.selectCostApportionmentList(cond);
        ExcelUtil<CostApportionmentDto> util = new ExcelUtil<>(CostApportionmentDto.class);
        return util.exportExcel(costApportionmentDtos, "销售订单出库单数据");
    }

    /**
     * 新增在产成本费用分摊
     * @param costApportionment
     * @return
     */
    @ApiOperation(value = "新增在产成本费用分摊", notes = "新增在产成本费用分摊信息")
    @ApiImplicitParam(name = "costApportionment", value = "新增在产成本费用分摊", dataType = "CostApportionmentDomain")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody CostApportionmentDomain costApportionment) {
        for (int i = 0; i < costApportionment.getCostApportionmentLists().size(); i++) {
            if (costApportionmentService.selectCostApportionmentByPlanSelfId(costApportionment.getCostApportionmentLists().get(i).getPlanSelfId())) {
                return AjaxResult.error("该生产计划存在费用分摊，无法重复添加");
            }
        }
        return toAjax(costApportionmentService.insertCostApportionment(costApportionment));
    }

    /**
     *
     * 更新在产成本费用分摊
     * @param costApportionment
     * @return
     */
    @ApiOperation(value = "修改在产成本费用分摊", notes = "修改在产成本费用分摊")
    @ApiImplicitParam(name = "costApportionment", value = "修改在产成本费用分摊", dataType = "CostApportionmentDomain")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody CostApportionmentDomain costApportionment) {
        try {
            costApportionmentService.updateCostApportionment(costApportionment);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除在产成本费用分摊
     * 仅修改删除标志位
     * @param costApportionment
     * @return
     */
    @ApiOperation(value = "删除在产成本费用分摊", notes = "删除在产成本费用分摊,仅删除标志位")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody CostApportionmentDomain costApportionment) {

        return toAjax(costApportionmentService.updateCostApportionmentDeleteFlag(costApportionment));
    }

    /**
     * 获取在产成本费用分摊明细
     * @param cond
     * @return
     */
    @ApiOperation(value = "查询明细列表",notes = "查询明细列表")
    @PostMapping("/detail")
    public TableDataInfo detail(@RequestBody CostApportionmentCond cond) {
        startPage();
        List<CostApportionmentListDto> costApportionmentListDtos = costApportionmentService.selectCostApportionmentListList(cond);
        return getDataTable(costApportionmentListDtos);
    }
}
